mdopt
markdown option parsing
Useful in combination with marked-man to ensure your man pages
are always up to date by making the docs a core part of the cli's
runtime. (related: package.json man).
Check out the test cases, or view the example below to get an idea
of what a properly formatted options section looks like.
Example
cli.js
:
const mdopt = require('mdopt');
const { readFileSync } = require('fs');
const md = readFileSync('path.md', {
encoding: 'utf8'
});
const argv = process.argv.slice(2)
const argv = mdopt(argv, md);
console.log(argv);
argv
is a variable that holds a minimist object that has
been populated (aliases, defaults) according to the provided markdown
file.
A sample session might look like this:
$ node cli.js
Error: Missing required option demand
$ node cli.js --demand
Error: Missing required option pghost
$ PGHOST=localhost node demo.js --demand hey hey you you
{ _: [ 'hey', 'hey', 'you', 'you' ],
f: false,
flag: false,
home: '/home/jay',
demand: true,
default: 'foo',
d: 'foo',
pghost: 'localhost' }
And finally what the spec.md
file looks like demonstrating
all available options:
spec.md
# demo -- A demonstration of the features & formatting
## SYNOPSIS
sample [flags] `<first>` `<last>` `[pet]`
## OPTIONS
### -f, --flag
This is a boolean flag as there is no values that
follow the flag. It can be accessed with $('f') or
$('flag')
### --anything ANYTHING
This flag expects a value to come after it. It can be a
number, a string, etc. The type will be auto detected
and the value of $('anything') will be that value.
### -s "VALUE", --string "VALUE"
Same as above, except that the value placeholder is in
quotes meaning that no type detection is performed, and
it is kept as a string. Give it `000123` and it will
remain `000123` vs. converting it to a number resulting
in `123`.
### --default=SOMETHING, -d SOMETHING (default=foo)
It is also possible to set default values.
### --home (default=$HOME)
And use environment variables to set those
defaults. Any default value beginning with a `$` will
be treated as an environment variable.
### --demand (required)
We can also demand that a flag should be set.
### --pghost "URI" (required, default=$PGHOST)
Combining required and using environment variables as
defaults is a good way to ensure that the value will be
set one way or another.
## AUTHORS
... Another section
## BUGS
... Another section. Add as many sections as you want.